package com.lc.qianrenshiye.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.GoodsListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/11/25.
 */
public class HomeShowAdapter extends ArrayAdapter<GoodsListGet.GoodsEntity>{

    private Context context;
    private List<GoodsListGet.GoodsEntity> showLists;
    private LayoutInflater layoutInflater;
    public int showSelPosition;

    public HomeShowAdapter(Context context, List<GoodsListGet.GoodsEntity> showLists) {
        super(context, 0, showLists);

        this.context = context;
        this.showLists = showLists;
        layoutInflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        GoodsListGet.GoodsEntity goodsEntity = getItem(position);

        ViewHolder viewHolder;

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_home_show_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        if (showSelPosition == position) {

            viewHolder.show_list_ll.setBackgroundColor(ContextCompat.getColor(context, R.color.home_list_sel_bg_color));

        } else {

            viewHolder.show_list_ll.setBackgroundColor(ContextCompat.getColor(context, R.color.white));

        }

        viewHolder.show_list_name_tv.setText(goodsEntity.title);
        viewHolder.show_list_price_tv.setText(goodsEntity.deprice);
        viewHolder.show_list_sale_num_tv.setText(goodsEntity.num);

        GlideLoader.getInstance().get(context,goodsEntity.picurl, viewHolder.show_list_iv, R.mipmap.img_holder, GlideLoader.TYPE_IMAGE_FILLET);

        return convertView;
    }

    class ViewHolder {

        @BoundView(R.id.show_list_ll) private LinearLayout show_list_ll;
        @BoundView(R.id.show_list_iv) private ImageView show_list_iv;
        @BoundView(R.id.show_list_name_tv) private TextView show_list_name_tv;
        @BoundView(R.id.show_list_price_tv) private TextView show_list_price_tv;
        @BoundView(R.id.show_list_sale_num_tv) private TextView show_list_sale_num_tv;
    }

}
